package com.thib.htstvremote;

import java.util.ArrayList;

import android.R.bool;
import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.thib.adapters.CustomListAdapter;
import com.thib.adapters.CustomListAdapterRecorder;
import com.thib.models.Entrie;
import com.thib.models.Finished;


/**
 * Fragment that appears in the "content_frame"
 */
public class ItemFragment extends Fragment 
{
    public static final String ARG_ITEM_NUMBER = "item_number";
    public static final String ARG_ITEM_LST = "item_lst";
    private ListView mlst;
    public Boolean b = false;
    private int i;
    private int itemPosition;
    ArrayList<Entrie> lst1;
    ArrayList<Finished> lst2;
   
    public ItemFragment() {
        // Empty constructor required for fragment subclasses
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item, container, false);
        i = getArguments().getInt(ARG_ITEM_NUMBER);
        String item = getResources().getStringArray(R.array.planets_array)[i];
        
        ((TextView) rootView.findViewById(R.id.textView1)).setText(item);
        getActivity().setTitle(item);
        mlst =   ((ListView) rootView.findViewById(R.id.listView1listView1));
        ArrayList<Entrie> Lst = getArguments().getParcelableArrayList(ARG_ITEM_LST);
        if(Lst != null)
        { MysetAdap(Lst);} 
        return rootView;
    }
    
    public void MysetAdap(ArrayList<Entrie> lst)
    {
    	CustomListAdapter adapter = new CustomListAdapter(getActivity(),lst);    
    	
    	this.mlst.setAdapter(adapter);
    	this.lst1 = lst;
    }
    
   
    public void loadMoreListItems(int start)
    {
    	if(b)
    	{
    		((MainActivity)getActivity()).r.getEpg(start+1);
    		this.lst2.addAll(((MainActivity)getActivity()).r.arrayRecorder);
    		
    	}
    }
    
    public void MysetAdap2(ArrayList<Finished> lst)
    {
    	this.registerForContextMenu(mlst);
    	CustomListAdapterRecorder adapter = new CustomListAdapterRecorder(getActivity(),lst);   	
    	this.mlst.setAdapter(adapter);
    	this.mlst.setOnItemClickListener(new OnItemClickListener() {


			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				itemPosition = arg2;
				// TODO Auto-generated method stub
				//Toast.makeText(getActivity().getApplicationContext(), "msg: OnClickListener", Toast.LENGTH_SHORT).show();
				 mlst.showContextMenu();
		            
		           
			}
		});
    	
    	//Here is where the magic happens
    	this.mlst.setOnScrollListener(new OnScrollListener(){
    		//useless here, skip!
    		@Override
    		public void onScrollStateChanged(AbsListView view, int scrollState) {}
    		//dumdumdum
    		@Override
    		public void onScroll(AbsListView view, int firstVisibleItem,
    			int visibleItemCount, final int totalItemCount) {
    			//what is the bottom iten that is visible
    			int lastInScreen = firstVisibleItem + visibleItemCount;
    			//is the bottom item visible & not loading more already ? Load more !
    			if((lastInScreen == totalItemCount)){
    				
    				new Thread(new Runnable() {
    	      		    public void run() {
    	      		    	loadMoreListItems(totalItemCount);
    	      		    	getActivity().runOnUiThread(new Runnable() {
    	      			   public void run() {
    	      				 BaseAdapter ba = (BaseAdapter)mlst.getAdapter();
 		       	    		ba.notifyDataSetChanged();
    	    		        	
    	      			   }});
    	      		    }}).start();
    			}
    		}
    	});
    	
    	this.lst2 = lst;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        switch (i){
                
                case 2:
               	 	inflater.inflate(R.menu.recording_menu, menu);
               	break;
                case 3:
               	 inflater.inflate(R.menu.recorded_menu, menu);
               	break;
                case 4:
               	 inflater.inflate(R.menu.failed_menu, menu);
               	break;
                case 5:
                  	 inflater.inflate(R.menu.record_menu, menu);
                  	break;
                default:
                	break;
                                    	
        }
        
       
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
       
        final Finished mitem;
        final Entrie mitemE;
        final MainActivity ativity =   (MainActivity) getActivity();       
        switch (item.getItemId()) {
            case R.id.show_info:
                //editNote(info.id);
            	//Toast.makeText(getActivity().getApplicationContext(), "msg:"+info.id, Toast.LENGTH_SHORT).show();
                return true;
            case R.id.delete:            	
            	mitem = (Finished)this.mlst.getItemAtPosition(itemPosition);
            	new Thread(new Runnable() {
	      		    public void run() {
	      		    	ativity.r.removeFinishedRecord(mitem.getId());
	                    //deleteNote(info.id);
	                	//Toast.makeText(getActivity().getApplicationContext(), "msg:"+info.id, Toast.LENGTH_SHORT).show();
	                	lst2.remove(itemPosition);
	                	getActivity().runOnUiThread(new Runnable() {
	    	      			   public void run() {
	    	      				 BaseAdapter ba = (BaseAdapter)mlst.getAdapter();
	 		       	    		ba.notifyDataSetChanged();
	    	    		        	
	    	      			   }});
	      		    }}).start();           	
                return true;               
            case R.id.recorde:            	
            	mitem = (Finished)this.mlst.getItemAtPosition(itemPosition);
            	new Thread(new Runnable() {
	      		    public void run() {     		    	
	      		    	ativity.r.recordEpg(mitem.getId());
	      		    }}).start();
                return true;                
            case R.id.stop_recording:
            	mitem = (Finished)this.mlst.getItemAtPosition(itemPosition);
            	
            	new Thread(new Runnable() {
	      		    public void run() {
	      		    	 ativity.r.removeRecord(mitem.getId());
	                    //deleteNote(info.id);
	                	//Toast.makeText(getActivity().getApplicationContext(), "msg:"+info.id, Toast.LENGTH_SHORT).show();
	                	lst2.remove(itemPosition);
	                	getActivity().runOnUiThread(new Runnable() {
	    	      			   public void run() {
	    	      				 BaseAdapter ba = (BaseAdapter)mlst.getAdapter();
	 		       	    		ba.notifyDataSetChanged();
	    	    		        	
	    	      			   }});
	      		    }}).start();
            	return true;
            case R.id.delete_failed:
            	mitem = (Finished)this.mlst.getItemAtPosition(itemPosition);
            	new Thread(new Runnable() {
	      		    public void run() {
	      		    	ativity.r.removeFinishedRecord(mitem.getId());
	                	lst2.remove(itemPosition);
	                	
	                	getActivity().runOnUiThread(new Runnable() {
	    	      			   public void run() {
	    	      				 BaseAdapter ba = (BaseAdapter)mlst.getAdapter();
	 		       	    		ba.notifyDataSetChanged();
	    	    		        	
	    	      			   }});
	      		    }}).start();          	
            	return true;            	
            default:
                return super.onContextItemSelected(item);
        }
    }
}